﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SARS.Entity;

namespace SARS
{
    public partial class SiteMaster : MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        private void RemoveMenuItem(MenuItem node, Menu menu)
        {
            if (node.Parent != null)
            {
                MenuItem menuNode = node.Parent;

                menuNode.ChildItems.Remove(node);
            }
            else
            {
                menu.Items.Remove(node);
            }
        }

        protected void NavigationMenu_MenuItemDataBound(object sender, MenuEventArgs e)
        {
            if (Session["Roles"] != null && !e.Item.Value.Equals("Home") && Page.User.Identity.IsAuthenticated)
            {
                switch ((RoleType)Session["Roles"])
                {
                    case RoleType.Tutor:
                        if (e.Item.Value.Equals("Session") || e.Item.Value.Equals("Student Attendance")
                            || e.Item.Value.Equals("View Absence Reason Notify"))
                        {
                            break;
                        }
                        RemoveMenuItem(e.Item, (Menu)sender);
                        break;
                    case RoleType.Student:
                        if (!e.Item.Value.Equals("View Your Attendance"))
                        {
                           RemoveMenuItem(e.Item,(Menu)sender);
                        }
                        break;
                    case RoleType.Coordinator:
                        if (e.Item.Value.Equals("Group Management") || e.Item.Value.Equals("Create Group")
                            || e.Item.Value.Equals("Assign Student Into Group")
                            || e.Item.Value.Equals("Course")
                            || e.Item.Value.Equals("Assign Student Course")
                            || e.Item.Value.Equals("Session")
                            || e.Item.Value.Equals("Add Session")
                            || e.Item.Value.Equals("Assign New Student"))
                        {
                            break;
                        }
                        RemoveMenuItem(e.Item,(Menu)sender);
                        break;
                    case RoleType.Lecturer:
                        if (e.Item.Value.Equals("Session") || e.Item.Value.Equals("Student Attendance"))
                        {
                            break;
                        }
                        RemoveMenuItem(e.Item, (Menu)sender);
                        break;
                }
            }
            
            if (SiteMap.CurrentNode != null)
            {
                if (e.Item.Text == SiteMap.CurrentNode.Title)
                {
                    if (e.Item.Parent != null)
                    {
                        e.Item.Parent.Selected = true;
                    }
                    else
                    {
                        e.Item.Selected = true;
                    }
                }
            }
        }

    }
}